package com.lin.filegraph.utils.parser.astutil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 14:17
 */

public class FileManager {
    private String projectPath = null;
    private List<String> srcFilePathList;
    private List<String> classFilePathList;
    private List<String> jarFilePathList;

    public FileManager(String projectPath) {
        super();
        this.projectPath = projectPath;
        srcFilePathList = new ArrayList<>();
        classFilePathList = new ArrayList<>();
        jarFilePathList = new ArrayList<>();
        searchFilePaths();
    }

    public String getProjectPath() {
        return projectPath;
    }

    public List<String> getSrcFilePathList() {
        return srcFilePathList;
    }

    public List<String> getClassFilePathList() {
        return classFilePathList;
    }

    public List<String> getJarFilePathList() {
        return jarFilePathList;
    }

    private void searchFilePaths() {
//		Set<String> strDirList = DirectoryStorage.getRelPath(projectPath);
        File directory = new File(projectPath);
        ArrayList<File> directoryFiles = new ArrayList<>();
        directoryFiles.add(directory);
        while (!directoryFiles.isEmpty()) {
            File direct = directoryFiles.get(0);
            directoryFiles.remove(0);
            File[] files = direct.listFiles();
            if (files == null) {
                continue;
            }
            for (File file : files) {
                if (file.isDirectory()) {
                    directoryFiles.add(file);
                } else if (file.getName().endsWith(".java")) {
//					String strPath = file.getAbsolutePath();
//					String pathR = strPath.split(projectPath.replaceAll("/", "//"))[1]
//							.split(file.getName())[0];
//					int first = pathR.indexOf("/");
//					int end = pathR.lastIndexOf("/");
//					if (first == 0 && end == 0)
//						continue;
//					String RelativePath = pathR.substring(first + 1, end);
//					if (strDirList.contains(RelativePath)) {
                    srcFilePathList.add(file.getAbsolutePath());
//					}

                } else if (file.getName().endsWith(".class")) {
                    classFilePathList.add(file.getAbsolutePath());
                } else if (file.getName().endsWith(".jar")) {
                    jarFilePathList.add(file.getAbsolutePath());
                }
            }
        }
    }
}

